package com.tuanzi.loan.web.bean.system;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.tuanzi.loan.business.entity.CurrentUser;
import com.tuanzi.loan.business.entity.LoginUser;
import com.tuanzi.loan.business.entity.system.SystemUser;
import com.tuanzi.loan.business.service.system.SystemUserService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.web.configuration.JSFInfoBean;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @author Ray 2017/09/21
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class SystemUserSettingBean extends JSFInfoBean<SystemUser> {

	private static final long serialVersionUID = -2152778554108785342L;

	@Autowired
	private SystemUserService systemUserService;

	private Integer activeIndex;
	private String oldPassword;
	private String newPassword;
	private String confirmPassword;
	private LoginUser loginUser;

	@PostConstruct
	private void init() {
		activeIndex = 0;
		loginUser = CurrentUser.get();
	}

	public void onChangePassword() {
		systemUserService.changePassword(oldPassword, newPassword, confirmPassword);
		addSuccessfullyMessage();
	}

	public void onSaveUserInfo() {
		SystemUser systemUser = systemUserService.findOne(loginUser.getUserId());
		if (systemUser == null) {
			addErrorMessage("没找到对应的用户");
			return;
		}

		systemUser.setName(loginUser.getName());
		systemUser.setEmail(loginUser.getEmail());
		systemUser.setPhone(loginUser.getPhone());
		systemUserService.saveOrUpdate(systemUser);

		addSuccessfullyMessage();
	}

}
